<template>
  <!-- category（分类）信息管理页 -->
  <div class="main">
    <!-- 分类信息列表渲染 -->
    <el-table
      :data="userRoleList"
      style="width: 1002px; margin: 0 auto"
      max-height="352"
      stripe
      border
      :header-cell-style="headerStyle"
    >
      <!-- ID项 -->
      <el-table-column prop="userId" label="用户ID" width="200">
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="200">
      </el-table-column>
      <!-- 名称项 -->
      <el-table-column prop="roleNames" label="用户角色" width="200">
      </el-table-column>
      <!-- 操作栏 -->
      <el-table-column label="操作" width="400">
        <template slot-scope="scope">
          <!-- 编辑按钮 -->
          <el-button
            type="primary"
            :disabled="userRoleList[scope.$index].userId == 1"
            @click="editUserRoleOpen(scope.row)"
            icon="el-icon-edit"
            size="small"
          ></el-button>
          <!-- 删除按钮 -->
          <el-button
            :disabled="userRoleList[scope.$index].userId == 1"
            @click="deleteRow(scope.$index, userRoleList)"
            size="small"
            type="primary"
            icon="el-icon-delete"
          >
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加提示输入名称dialog -->
    <el-dialog
      title="请输入必要信息："
      :visible.sync="editDialogFlag"
      width="30%"
      center
      top="30vh"
    >
      <span class="title">用户ID：</span>
      <el-input v-model="editUserRole.userId" size="small" disabled> </el-input>
      <span class="title">用户名称：</span>
      <el-input
        v-model="editUserRole.username"
        size="small"
        disabled
      ></el-input>
      <span class="title">角色选择：</span>
      <el-select v-model="editUserRole.roleId" placeholder="请选择">
        <el-option
          v-for="item in roles"
          :key="item.roleId"
          :label="item.roleName"
          :value="item.roleId"
        >
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogFlag = false">取 消</el-button>
        <el-button type="primary" @click="edit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userRoleList: [],
      editUserRole: {},
      roles: [],
      editDialogFlag: false,
    };
  },
  mounted() {
    this.getPowers();
    this.getRoles();
  },
  methods: {
    headerStyle() {
      return "color: #000;background-color: #c5464a;height:60px;";
    },
    getPowers() {
      this.$http.get("user/userRole/list").then((response) => {
        this.userRoleList = response.data.result;
      });
    },
    getRoles() {
      this.$http.get("role/list").then((response) => {
        this.roles = response.data.result;
        this.roles.forEach((element) => {
          element.powerListOptions = JSON.parse(element.powerListOptions);
        });
      });
    },
    deleteRow(index, data) {
      console.log(data);
      const username = data[index].username;
      const userId = data[index].userId;
      const roleName = data[index].roleNames;
      this.$confirm(
        "此操作将永久删除用户名为【" +
          username +
          "】的角色【" +
          roleName +
          "】, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          showCancelButton: true,
          type: "warning",
        }
      )
        .then(() => {
          data.splice(index, 1);
          this.userRoleList = data;
          // 删除数据库中的对应项
          this.$http.post("user/userRole/delete", {userId:userId}).then((response) => {
            this.getPowers();
            this.$message.success("删除成功");
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    editUserRoleOpen(row) {
      this.editDialogFlag = true;
      this.editUserRole = JSON.parse(JSON.stringify(row));
    },
    edit() {
      let data = {};
      data.userId = this.editUserRole.userId;
      data.roleId = this.editUserRole.roleId;
      this.$http.post("user/userRole/modify", data).then((response) => {
        this.getPowers();
        this.$message.success("修改成功");
      });
      this.editDialogFlag = false;
      this.editUserRole = {};
    },
  },
};
</script>

<style scope>
.main {
  overflow: hidden;
}
.el-input__inner {
  padding: 0 !important;
}
.addButton {
  margin-top: 12px !important;
  float: right;
  right: 30px;
}
</style>
